package
{
	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.filesystem.File;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	
	public class Log
	{
		/**
		 * 写入日志
		 * @param	content
		 */
		public static function WriteLog(content:String):void
		{
			var logDir:File = new File(File.applicationDirectory.resolvePath("log").nativePath);
			if (!logDir.exists) logDir.createDirectory();
			//存在同名文件
			if (logDir.exists && !logDir.isDirectory) return;
			var date:Date = new Date();
			var fileName:String = (date.fullYear.toString() + "-" + (date.month + 1).toString() + "-" + date.date.toString());
			var logURL:String = File.applicationDirectory.resolvePath("log/" + fileName + ".log").nativePath;
			var file:File = new File(logURL);
			var fs:FileStream = new FileStream();
			fs.open(file, "append");
			fs.writeUTFBytes(content + "\n");
			fs.close();
		}
		
		/**
		 * 删除7天前的日志
		 */
		public static function ClearLog():void
		{
			var logDir:File = new File(File.applicationDirectory.resolvePath("log").nativePath);
			if (!logDir.exists || !logDir.isDirectory) return;
			var allLog:Array = logDir.getDirectoryListing();
			allLog.forEach(function(file:File, b:*, c:*):void
			{
				if (file.extension != "log") return;
				if ((new Date).date - file.modificationDate.date > 7 * 24 * 60 * 60 * 1000)
				{
					file.deleteFile();
					WriteLog("删除日志文件:" + file.name);
				}
			});
		}
	}
}
